import GCJ_CLASS_TYPE = require('./../../common/GCJ_CLASS_TYPE');
import Serializer = require('./../base/Serializer');
import SJGHead = require('./../cont/SJGHead');
import SJGDocumentBody = require('./../cont/SJGDocumentBody');
import GlobalData = require('./../../z/GlobalData');
import Parser = require('./../base/Parser');
var CONFIG=require('../../../../common/config');
export  = SJGDocument;

/**
 *
 * @author 
 *
 */
class SJGDocument extends Serializer{
    public head: SJGHead = new SJGHead();
    public body: SJGDocumentBody = new SJGDocumentBody();
    public max_id: number = 0;
    
    
    public constructor(g?) {
        super(g);
        if(g) { 
            this.body.setGlobalData(g);// 为兼容nodejs代码，传递全局域
        }
        
        this.ct = GCJ_CLASS_TYPE.CLASS_TYPE_SJGDocument;
    }
    
    // 从json对象(注意不是json字符串)中解析
    public fromJson(jsonObj, versionId) { 
        super.fromJson( jsonObj, versionId );
        
        this.head = Parser.fromUnknownJson( jsonObj.head, versionId );
        this.body = Parser.fromUnknownJson( jsonObj.body, versionId , this.getGlobalData());
        this.max_id = jsonObj.max_id;
        
        
        
    }
    // 转成json字符串
    public toJson(withPages:boolean = true):string {
        // 去掉父json字符串的头尾
        var superJson = super.toJson();
        superJson = superJson.substr(1,superJson.length-2);
                                
        var ret = '{';
        ret +=superJson;
        ret += ',';    
        ret += ('"head":'+ this.head.toJson());
        ret += ',';
        ret += ('"body":'+  this.body.toJson(withPages) );
        ret += ',';
        ret += ('"max_id":'+  this.max_id );

        ret += "}";
        return ret;
    }
    
    // 新建空白Page
    // 参数：［可选］pageIndex页偏移，如果不传入，则添加到末尾；注意：不是id
    // 参数：［可选］pageJson页面json
    // 参数：［可选］mstPageId 母版page_id
    // 返回：新增页的id
    public addPage(pageIndex?: number,pageJson?: string,mstPageId?: number): number {
        return this.body.addPage(pageIndex,pageJson,mstPageId);
    }
    // 删除页
    // 参数：pageIndex页偏移，注意：不是id
    public deletePage(pageIndex:number){
        return this.body.deletePage( pageIndex );
    }
    //  升级数据，需要升级到接口实现
    public upgradeData() {
        this.body.upgradeData();
    }
    
    // 增加测试页
    public addTestData(){
        this.body.addTestData();        
    }
    // 增加测试页2
    public addTestData2() {
        var json: string='{"ct":403,"head":{"ct":401,"flag":"SJG_T","ver":1,"author":"","create_time":"","update_time":"","name":""},"body":{"ct":404,"pos":{"ct":2,"x":0,"y":0,"an":0,"w":1600,"h":900},"pages":[{"ct":402,"describe":{"ct":4,"index":0,"id":2,"play_time":1,"show_style":0,"disappear_style":0},"root":{"ct":607,"id":1,"pos":{"ct":2,"x":0,"y":0,"an":0,"w":1600,"h":900},"al":1,"constrain":{"ct":1,"scale":1,"whratio":1},"children":[{"ct":606,"id":7,"pos":{"ct":2,"x":123.07692307692311,"y":257.23076923076917,"an":0,"w":81,"h":60},"al":1,"color":{"ct":202,"r":0,"g":0,"b":0},"constrain":{"ct":1,"scale":1,"whratio":1},"fn":"","fs":0,"bi":0,"po":1,"cs":4,"ls":8,"te":"fef","td":0,"pc":{"paras":[{"chars":[{"paths":[{"points":[[21.000,-46.000,8],[19.000,-47.000,2],[17.000,-47.000,2],[12.000,-47.000,2],[12.000,-40.000,2],[12.000,-35.000,1],[20.000,-35.000,1],[20.000,-30.000,1],[12.000,-30.000,1],[12.000,0.000,1],[6.000,0.000,1],[6.000,-30.000,1],[0.000,-30.000,1],[0.000,-35.000,1],[6.000,-35.000,1],[6.000,-40.000,1],[6.000,-46.000,2],[9.000,-49.000,2],[12.000,-52.000,2],[17.000,-52.000,2],[20.000,-52.000,2],[21.000,-51.000,2],[21.000,-46.000,1]],"w":0,"h":0}],"w":21,"h":52,"x":0,"y":-52,"bx":0,"by":60},{"paths":[{"points":[[31.000,-16.000,8],[6.000,-16.000,1],[7.000,-10.000,2],[9.000,-7.000,2],[12.000,-4.000,2],[18.000,-4.000,2],[23.000,-4.000,2],[28.000,-8.000,2],[28.000,-2.000,1],[24.000,1.000,2],[16.000,1.000,2],[9.000,1.000,2],[4.000,-4.000,2],[0.000,-9.000,2],[0.000,-17.000,2],[0.000,-25.000,2],[4.000,-30.000,2],[9.000,-35.000,2],[16.000,-35.000,2],[23.000,-35.000,2],[27.000,-31.000,2],[31.000,-27.000,2],[31.000,-19.000,2],[31.000,-16.000,1]],"w":0,"h":0},{"points":[[25.000,-21.000,8],[24.000,-25.000,2],[22.000,-27.000,2],[20.000,-30.000,2],[16.000,-30.000,2],[12.000,-30.000,2],[9.000,-27.000,2],[7.000,-25.000,2],[6.000,-21.000,2],[25.000,-21.000,1]],"w":0,"h":0}],"w":31,"h":36,"x":0,"y":-35,"bx":25,"by":60},{"paths":[{"points":[[21.000,-46.000,8],[19.000,-47.000,2],[17.000,-47.000,2],[12.000,-47.000,2],[12.000,-40.000,2],[12.000,-35.000,1],[20.000,-35.000,1],[20.000,-30.000,1],[12.000,-30.000,1],[12.000,0.000,1],[6.000,0.000,1],[6.000,-30.000,1],[0.000,-30.000,1],[0.000,-35.000,1],[6.000,-35.000,1],[6.000,-40.000,1],[6.000,-46.000,2],[9.000,-49.000,2],[12.000,-52.000,2],[17.000,-52.000,2],[20.000,-52.000,2],[21.000,-51.000,2],[21.000,-46.000,1]],"w":0,"h":0}],"w":21,"h":52,"x":0,"y":-52,"bx":60,"by":60}],"w":81,"h":60}],"w":81,"h":60}},{"ct":602,"id":9,"pos":{"ct":2,"x":564.9230769230771,"y":164.92307692307688,"an":0,"w":500,"h":500},"al":1,"mask":{"paths":[{"points":[[250.000,0.000,8],[390.000,0.000,3],[500.000,110.000,3],[500.000,250.000,3],[500.000,390.000,3],[390.000,500.000,3],[250.000,500.000,3],[110.000,500.000,3],[0.000,390.000,3],[0.000,250.000,3],[0.000,110.000,3],[110.000,0.000,3],[250.000,0.000,3]],"w":0,"h":0}],"w":500,"h":500,"x":0,"y":0},"constrain":{"ct":1,"scale":1,"whratio":1},"source":"http://7xogoq.com1.z0.glb.clouddn.com/XXX.jpg"}]},"replaces":[]},{"ct":402,"describe":{"ct":4,"index":1,"id":4,"play_time":1,"show_style":0,"disappear_style":0},"root":{"ct":607,"id":3,"pos":{"ct":2,"x":0,"y":0,"an":0,"w":1600,"h":900},"al":1,"constrain":{"ct":1,"scale":1,"whratio":1},"children":[]},"replaces":[]},{"ct":402,"describe":{"ct":4,"index":2,"id":6,"play_time":1,"show_style":0,"disappear_style":0},"root":{"ct":607,"id":5,"pos":{"ct":2,"x":0,"y":0,"an":0,"w":1600,"h":900},"al":1,"constrain":{"ct":1,"scale":1,"whratio":1},"children":[]},"replaces":[]}]},"max_id":6}';
        this.fromJson(JSON.parse(json),0);
    }
    
    
//SJG_MARK_APPEND--------------------
    // 初始化用户的可替换数组
    public initUserReplaces(jsonObj, versionId){
        this.body.initUserReplaces(jsonObj, versionId);
    }
    // 增加用户的可替换数组－只有基本属性
    public addTestDataOfUserReplaces(){
        this.body.addTestDataOfUserReplaces();
    }
    // 排版
    public typeset(callback:Function){
        this.body.typeset(callback);
    }

}//SJG_MARK_END-------------------------
